var purchaseCourseId = getQueryVariable('uid');
var teacherId = getQueryVariable('teacherId');
var weeks = ['-', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'];
jQuery(document).ready(function () {
    $('#kit-search-more').on('click', function () {
        $('.kit-search-mored').toggle();
    });


    initInfo();
    var col = [
        {
            field: 'Number',
            title: '序号',
            align: 'center',
            formatter: function (value, row, index) {
                return index + 1;
            }
        },
        {
            title: '上课日期',
            field: 'classDate',
            align: 'center',
            valign: 'middle'
        },
        {
            title: '外教名称',
            field: 'name',
            align: 'center',
            valign: 'middle'
        },
        {
            title: '上课时间（开始）',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                return row.classTime + '-' + row.classTimeEnd + '(' + weeks[row.classWeek] + ')';
            }
        },
        {
            title: '课时（分钟）',
            field: 'classLength',
            align: 'center',
            valign: 'middle'
        },
        {
            title: '教材',
//		field:'material',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                return '-';
            }
        },
        {
            title: '状态',
            field: 'stateName',
            align: 'center',
            // valign: 'middle'
            formatter: function (value, row, index) {
                var st = '';
                switch (row.state) {
                    case 0:
                        st = '默认';
                        break;
                    case 1:
                        st = '无课';
                        break;
                    case 2:
                        st = '学生请假';
                        break;
                    case 3:
                        st = '学生缺席';
                        break;
                    case 4:
                        st = '外教请假';
                        break;
                    case 5:
                        st = '外教缺席';
                        break;
                    case 6:
                        st = '补课';
                        break;
                    case 7:
                        st = '完成';
                        break;
                    case -1:
                        st = '';
                        break;
                }
                return st;
            }
        },
//	{
//		title:'状态',
//		field:'state',
//		align: 'center',
//		valign: 'middle'
//	},
//	{
//		title:'教师评语（上课表现）',
//		field:'performance',
//		align: 'center',
//		valign: 'middle'
//	},
        {
            title: '上课内容/作业',
//		field:'homework',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                var t = '';
                if (row.performance) {
                    t += 'Performance:(' + row.performance + ');';
                }
                if (row.homework) {
                    t += 'Homework:(' + row.homework + ')';
                }
                return t;
            }

        },
        {
            title: '下期学习安排',
            field: 'contentNextLecture',
            align: 'center',
            valign: 'middle'
        },
        {
            title: '视频',
            formatter:function(value,row,index){
                 if(row.video !=null){
                       return '<a href="javascript:;" class="layui-btn layui-btn-sm" onclick="getCourselogRecording(\'' + row.id + '\')">获取视频</a>';
                }
            }
        },
        {
            title: '操作',
            formatter: function (value, row, index) {
                return '<a href="javascript:;" class="layui-btn layui-btn-sm" onclick="edit(\'' + row.id + '\')">编辑</a>';
//			'<a href="javascript:;" class="layui-btn layui-btn-sm" onclick="manger(\''+row.uniqueid+'\')">课程管理</a>'+
//			'<a href="javascript:;" class="layui-btn layui-btn-sm" onclick="tdate(\''+row.uniqueid+'\')">日程</a>'+
//			'<a href="javascript:;" onclick="doDelete(\''+row.uniqueid+'\')" class="layui-btn layui-btn-sm
// layui-btn-danger"><i class="layui-icon">&#xe640;</i> 删除</a>';
            }
        }
    ];

    //------------------------------------------加载列表
    $('#_table').bootstrapTable({
                                    url: baseUrl + '/curriculumSchedules/getView?purchaseCourseId=' + purchaseCourseId,
                                    'method': "GET",
                                    locale: 'zh-CN',
                                    detailView: false,
                                    sidePagination: "cilent", //服务端处理分页
                                    striped: true,//隔行变色
                                    undefinedText: "",
                                    pagination: true,
                                    singleSelect: false,
                                    pageSize: pageList[0],
                                    cache: false, //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                                    pageNumber: 1,
                                    pageList: pageList,
                                    search: false, //不显示 搜索框
                                    uniqueId: "id", //每一行的唯一标识，一般为主键列
                                    showColumns: false, //不显示下拉框（选择显示的列）
                                    undefinedText: '',
                                    columns: col,
                                    queryParams: function (params) {
                                        return {
                                            pageNum: this.pageNumber,
                                            pageSize: this.pageSize,
                                            sortOrder: params.order
                                        }
                                    },

                                });
})
var check = function () {

}

var initInfo = function () {
    $.get(baseUrl + '/curriculumSchedules/countByState?pcid=' + purchaseCourseId, function (data) {
        data = data.data;
        for (var i = 0; i < data.length; i++) {
            if (data[i].state == 0) {
                continue;
            }
            $('#state' + data[i].state).html(data[i].count + '');
        }
    });
    $.ajax({
               url: baseUrl + '/purchaseCourseViews/' + purchaseCourseId,
               type: 'get',
               async: false,
               success: function (datas) {
                   console.log(datas);
//			var data = datas.rows[0];
//			$('#stuname').html(data.studentName);
                   var data = datas.data;
                   data.hoursEnrolled = (data.hoursEnrolled / 60).toFixed(2);
                   data.hoursRemaining = (data.hoursRemaining / 60).toFixed(2);
                   data.hoursCompleted = (data.hoursCompleted / 60).toFixed(2);
                   $.link(true, ".layui-form", data);
               }
           });
}
var doDelete = function () {
    var rows = $('#_table').bootstrapTable('getSelections');
    if (rows == [] || rows == null || rows.length == 0) {
        parent.layer.alert('请选择要编辑的数据！', {
            skin: 'layui-layer-molv', //样式类名
            closeBtn: 0,
            anim: 4,
            time: 2000
        });
        return;
    }
    if (rows.length != 1) {
        parent.layer.alert('请选择单条数据进行编辑！', {
            skin: 'layui-layer-molv', //样式类名
            closeBtn: 0,
            anim: 4,
            time: 1800
        });
        return;
    }
    $.ajax({
               type: "delete",
               url: baseUrl + '/curriculumScheduleViews/' + rows[0].id,
               async: true,
               success: function (data) {
                   alert(data.msg);
                   $('#_table').bootstrapTable('refresh');
               }
           });
}
var insert = function () {
    parent.layer.open({
                          //				title: false,
                          maxmin: true,
                          type: 2,
                          area: modalArea,
                          content: './views/form/CurriculumScheduleViewEdit.html',
                          btn: ['保存', '取消'],
                          yes: function (index, layero) {
                              var data = $(layero).find("iframe")[0].contentWindow.callback_save();
                              console.log(layero);
                              $.ajax({
                                         url: baseUrl + '/curriculumScheduleViews',
                                         type: 'POST',
                                         data: data,
                                         success: function (data) {
                                             alert(data.msg);
                                             parent.layer.close(index);
                                             $('#_table').bootstrapTable('refresh');
                                         }
                                     });
                          },
                          btn2: function (index, layero) {
                              //按钮【按钮二】的回调
                              console.log(layero);
                              //return false 开启该代码可禁止点击该按钮关闭
                          }
                      });
}

function makeClass(purchaseCourseId) {
    layer.open({
                   title: 'Make Class',
                   type: 1,
                   closeBtn: false,
                   shift: 5,
                   shadeClose: true,
                   btn: ['提交', '取消'],
                   content:
                       "<div style='width:350px;'><div style='width:320px;margin-left: 3%;' class='form-group has-feedback'><p>增加课时</p><input id='hourClass' class='form-control' type='text' name='classHours' value='1'/></div>",
                   // "<button style='margin-top:5%;' type='button' class='btn btn-block btn-success btn-lg'
                   // onclick='updateAward('"+purchaseCourseId+"')'>提交</button></div>",
                   yes: function (index, layero) {
                       var hourClass = $("#hourClass").val();
                       $.ajax({
                                  type: "get",
                                  url: baseUrl + "/curriculumSchedules/picd?picd=" + purchaseCourseId + "&hoursClass="
                                       + hourClass,
                                  success: function (data) {
                                      parent.layer.close(index);
                                      $('#_table').bootstrapTable('refresh');
                                  }
                              });
                       parent.layer.close(index);
                   },
                   btn2: function (index, layero) {
                       parent.layer.close(index);
                   }
               });
}

function updateAward(purchaseCourseId) {
    var hourClass = $("#hourClass").val();
    $.ajax({
               type: "get",
               url: baseUrl + "/curriculumSchedules/picd?picd=" + purchaseCourseId + "&hoursClass=" + hourClass,
               success: function (data) {
                   var updateSuccess = data.data;
                   if (updateSuccess) {
                       layer.msg('修改成功', {
                           time: 500, //0.5s后自动关闭
                           // btn: ['明白了', '知道了']
                       });
                       refreshPage();
                   } else {
                       layer.msg('修改失败', {
                           time: 500, //20s后自动关闭
                           // btn: ['明白了', '知道了']
                       });
                   }
               }
           });
    layer.closeAll();
}

function getCourselogRecording(id) {
    parent.layer.open({
                          //				title: false,
                          maxmin: true,
                          type: 2,
                          area: modalArea,
                          content: './views/form/CurriculumScheduleViewEdit1.html?id=' + id, //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content:
                                                                                             // ['http://sentsin.com',
                                                                                             // 'no']
                          btn: ['保存', '取消'],
                          yes: function (index, layero) {
                              var data = $(layero).find("iframe")[0].contentWindow.callback_save();
                              console.log(layero);
                              $.ajax({
                                         url: baseUrl + '/curriculumSchedules/' + id,
                                         type: 'post',
                                         data: data,
                                         success: function (data) {
                                             alert(data.msg);
                                             parent.layer.close(index);
                                             $('#_table').bootstrapTable('refresh');

                                             $.ajax({
                                                        url: baseUrl + '/purchaseCourseViews/' + purchaseCourseId,
                                                        type: 'get',
                                                        async: false,
                                                        success: function (datas) {
                                                            console.log(datas);
                                                            var data = datas.data;
                                                            data.hoursEnrolled = data.hoursEnrolled / 60;
                                                            data.hoursRemaining = data.hoursRemaining / 60;
                                                            data.hoursCompleted = data.hoursCompleted / 60;
                                                            data.hourShengyu = data.hoursEnrolled - data.hoursRemaining;

                                                            $.link(true, ".layui-form", data);
                                                        }
                                                    });

                                         }
                                     });
                          },
                          btn2: function (index, layero) {
                          }
                      });
}

function getCourselogRecording(id) {
    parent.layer.open({
                          //				title: false,
                          maxmin: true,
                          type: 2,
                          area: modalArea,
                          content: './views/form/CurriculumScheduleViewEdit1.html?id=' + id, //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content:
                                                                                             // ['http://sentsin.com',
                                                                                             // 'no']
                          btn: ['保存', '取消'],
                          yes: function (index, layero) {
                              var data = $(layero).find("iframe")[0].contentWindow.callback_save();
                              console.log(layero);
                              $.ajax({
                                         url: baseUrl + '/curriculumSchedules/' + id,
                                         type: 'post',
                                         data: data,
                                         success: function (data) {
                                             alert(data.msg);
                                             parent.layer.close(index);
                                             $('#_table').bootstrapTable('refresh');

                                             $.ajax({
                                                        url: baseUrl + '/purchaseCourseViews/' + purchaseCourseId,
                                                        type: 'get',
                                                        async: false,
                                                        success: function (datas) {
                                                            console.log(datas);
                                                            var data = datas.data;
                                                            data.hoursEnrolled = data.hoursEnrolled / 60;
                                                            data.hoursRemaining = data.hoursRemaining / 60;
                                                            data.hoursCompleted = data.hoursCompleted / 60;
                                                            data.hourShengyu = data.hoursEnrolled - data.hoursRemaining;

                                                            $.link(true, ".layui-form", data);
                                                        }
                                                    });

                                         }
                                     });
                          },
                          btn2: function (index, layero) {
                          }
                      });
}

var edit = function (id) {
    parent.layer.open({
                          //				title: false,
                          maxmin: true,
                          type: 2,
                          area: modalArea,
                          content: './views/form/CurriculumScheduleViewEdit.html?id=' + id, //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content:
                                                                                            // ['http://sentsin.com',
                                                                                            // 'no']
                          btn: ['保存', '取消'],
                          yes: function (index, layero) {
                              var data = $(layero).find("iframe")[0].contentWindow.callback_save();
                              console.log(layero);
                              $.ajax({
                                         url: baseUrl + '/curriculumSchedules/' + id,
                                         type: 'post',
                                         data: data,
                                         success: function (data) {
                                             alert(data.msg);
                                             parent.layer.close(index);
                                             $('#_table').bootstrapTable('refresh');

                                             $.ajax({
                                                        url: baseUrl + '/purchaseCourseViews/' + purchaseCourseId,
                                                        type: 'get',
                                                        async: false,
                                                        success: function (datas) {
                                                            console.log(datas);
                                                            var data = datas.data;
                                                            data.hoursEnrolled = data.hoursEnrolled / 60;
                                                            data.hoursRemaining = data.hoursRemaining / 60;
                                                            data.hoursCompleted = data.hoursCompleted / 60;
                                                            data.hourShengyu = data.hoursEnrolled - data.hoursRemaining;

                                                            $.link(true, ".layui-form", data);
                                                        }
                                                    });

                                         }
                                     });
                          },
                          btn2: function (index, layero) {
                          }
                      });
}
var settimes = function () {
    if (teacherId) {
        parent.layer.open({
                              maxmin: true,
                              type: 2,
                              area: modalArea,
                              content: './views/UpdateClassTime.html?techerid=' + teacherId,
                              btn: ['确定', '取消'],
                              yes: function (index, layero) {
                                  var use_times = $(layero).find("iframe")[0].contentWindow.callback_save();
                                  $.ajax({
                                             url: baseUrl + '/purchaseCourses/updateClass?uniqueid=' + purchaseCourseId
                                                  + "&times=" + use_times,
                                             type: 'post',
                                             async: false,
                                             success: function (datas) {
                                                 parent.layer.close(index);
                                                 $('#_table').bootstrapTable('refresh');
                                             }
                                         });
                              },
                              btn2: function (index, layero) {
                              }
                          });
    } else {
        alert('请选择外教！');
    }
}
